package com.lantu.draw.framework;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * session过期、操作权限验证拦截器
 * @author wanghao
 *
 */
public class AuthInterceptor  extends AbstractInterceptor{
	
	
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		//取得请求的URL 
		String url = ServletActionContext.getRequest().getRequestURL().toString();  
		
		//拦截的action的名字
        String action = ai.getInvocationContext().getName();
        
        HttpServletResponse response=ServletActionContext.getResponse();  
        response.setHeader("Pragma","No-cache");            
        response.setHeader("Cache-Control","no-cache");     
        response.setHeader("Cache-Control", "no-store");     
        response.setDateHeader("Expires",0);  
        if (url.indexOf("login!checkAuth.action")!=-1 || url.indexOf("login!register.action")!=-1
        		||url.indexOf("shareicon.action")!=-1
        		||url.indexOf("mypaint.action")!=-1
        		||url.indexOf("myicon.action")!=-1)
        	
        
        {  
            return ai.invoke();  
        }  
        else{  //
            //验证Session是否过期  
            if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){  
                //session过期,转向session过期提示页,最终跳转至登录页面  
            	
               //return "jsonerror";  
            	throw new ErrorException("1-0001","login error","用户登录过期");
            }  
            else{  
            	//noPermission
                Object userId_obj=ServletActionContext.getRequest().getSession().getAttribute("userid");  
                //验证是否已经登录  
                if (userId_obj==null){  
                    //未登录
                	throw new ErrorException("1-0002","login error","未登录");
                    //return "jsonerror";  
                }else{                      
                    return ai.invoke();  
                                  
                }                  
            }              
        }  
	}

}
